package com.qs.eggs.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.account.LoginActivity;
import com.qs.eggs.user.UserInfoActivity;
import com.qs.eggs.view.activity.AddAddressActivity;
import com.qs.eggs.view.activity.AddAddressNewActivity;
import com.qs.eggs.view.activity.AddressManagementActivity;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;


public class AddressAdapter extends RecyclerView.Adapter<AddressAdapter.ViewHolder> implements View.OnClickListener{
    private LayoutInflater mInflater;
    private List<Map<String, Object>> addresslist=new ArrayList<>();
    private Context context;
    private String isdefault,id,province,city,area,village,custname,phone,address;
    public AddressAdapter(Context context, List<Map<String, Object>> addresslist)
    {
        mInflater = LayoutInflater.from(context);
        this.addresslist = addresslist;
        this.context=context;
    }
    private OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }

        private TextView name,number,address,bianji,delete;
        private RadioButton rb1;

    }

    @Override
    public int getItemCount()
    {
        return addresslist.size();
    }

    /**
     * 创建ViewHolder
     */
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View view = mInflater.inflate(R.layout.item_address,
                viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(view);
        view.setOnClickListener(this);
        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.number = (TextView) view.findViewById(R.id.number);
        viewHolder.address = (TextView) view.findViewById(R.id.address);
        viewHolder.rb1 = (RadioButton) view.findViewById(R.id.rb1);
        viewHolder.bianji = (TextView) view.findViewById(R.id.bianji);
        viewHolder.delete = (TextView) view.findViewById(R.id.delete);
        return viewHolder;
    }

    /**
     * 设置值
     */
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.name.setText(addresslist.get(position).get("custname").toString());
        viewHolder.number.setText(addresslist.get(position).get("phone").toString());
        isdefault=addresslist.get(position).get("isdefault").toString();
        id=addresslist.get(position).get("id").toString();
        province=addresslist.get(position).get("province").toString();
        city=addresslist.get(position).get("city").toString();
        area=addresslist.get(position).get("area").toString();
        village=addresslist.get(position).get("village").toString();
        custname=addresslist.get(position).get("custname").toString();
        phone=addresslist.get(position).get("phone").toString();
        address=addresslist.get(position).get("address").toString();
        viewHolder.address.setText(province+city+area+village+addresslist.get(position).get("address").toString());

        if (isdefault.equals("0")){
            viewHolder.rb1.setChecked(false);
        }else{
            viewHolder.rb1.setChecked(true);
            viewHolder.rb1.setClickable(false);
        }
        viewHolder.bianji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,AddAddressNewActivity.class);
                intent.putExtra("province",addresslist.get(position).get("province").toString());
                intent.putExtra("city",addresslist.get(position).get("city").toString());
                intent.putExtra("area",addresslist.get(position).get("area").toString());
                intent.putExtra("village",addresslist.get(position).get("village").toString());
                intent.putExtra("custname",addresslist.get(position).get("custname").toString());
                intent.putExtra("phone",addresslist.get(position).get("phone").toString());
                intent.putExtra("address",addresslist.get(position).get("address").toString());
                intent.putExtra("id",addresslist.get(position).get("id").toString());
                intent.putExtra("provinceid",addresslist.get(position).get("provinceid").toString());
                intent.putExtra("cityid",addresslist.get(position).get("cityid").toString());
                intent.putExtra("areaid",addresslist.get(position).get("areaid").toString());
                intent.putExtra("villageid",addresslist.get(position).get("villageid").toString());
                intent.putExtra("isdefault",addresslist.get(position).get("isdefault").toString());
                context.startActivity(intent);
            }
        });
        viewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                delete(addresslist.get(position).get("isdefault").toString(),addresslist.get(position).get("id").toString());
            }
        });
        viewHolder.rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    getdefault(addresslist.get(position).get("id").toString());
                }
            }
        });
    }
    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
    //删除地址
    private void delete(String isdefault,String id) {
        if (isdefault.equals("1")) {
            new AlertDialog.Builder(context)
                    .setTitle("删除地址")
                    .setMessage("默认地址不能删除，请先修改默认地址")
                    .setPositiveButton("确定",
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).show();
        } else {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("addressid", id);
            Gson gson = new Gson();
            String str = gson.toJson(map);
            OkGo.post(Constants.URL_BASE + "mallAddressManage?action=delCustAddress")
                    .params("data", str)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            System.out.print("删除地址==" + response);
                            if (s.equals("\"true\"")) {
                                AddressManagementActivity.instance.finish();
                                Intent intent = new Intent(context, AddressManagementActivity.class);
                                context.startActivity(intent);

                            } else {
                                Toast.makeText(context, "删除失败", Toast.LENGTH_LONG);
                            }
                        }

                    });
        }
    }
    //设置为默认地址
    private void getdefault(String id){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("id",id);
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"mallAddressManage?action=updCustAddressDefault")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("默认地址=="+response);
                        if (s.equals("\"true\"")){
                            AddressManagementActivity.instance.finish();
                            Intent intent=new Intent(context,AddressManagementActivity.class);
                            context.startActivity(intent);

                        }else{
                            Toast.makeText(context,"设置失败",Toast.LENGTH_LONG);
                        }
                    }

                });
    }
}
